package acheng1314.cn.a3dbuild2.view.activity.presenter;

import com.acheng.achengutils.gsonutil.GsonUtils;
import com.acheng.achengutils.mvp.presenter.BasePresenter;
import com.acheng.achengutils.utils.StringUtils;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;

import acheng1314.cn.a3dbuild2.bean.BuildBean;
import acheng1314.cn.a3dbuild2.hostApi.MyApi;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.DetailActivityController;

/**
 * Created by pang on 16/10/12.
 */
public class DetailActivityPresenter extends BasePresenter<DetailActivityController> {
    /**
     * 在子类的构造函数中，设定参数为model，这时候可以presenter调用接口来实现对界面的操作。
     *
     * @param model
     */
    public DetailActivityPresenter(DetailActivityController model) {
        super(model);
    }

    @Override
    public void initData() {

    }

    /**
     * get list data 4 net
     *
     * @param userId user id
     * @param pid    project id
     * @param token  the token for user
     */
    public void loadListData(String userId, String pid, String token) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(pid) || StringUtils.isEmpty(token)) {
            model.showDailog("内部数据错误,请重新运行程序!");
            return;
        }
        HttpParams params = new HttpParams();
        params.put("uid", userId);
        params.put("pid", pid);
        params.put("token", token);
        RxVolley.post(MyApi.BuildsApi, params, new HttpCallback() {

            @Override
            public void onFinish() {
                super.onFinish();
                model.disProgressD();
            }

            @Override
            public void onPreStart() {
                super.onPreStart();
                model.showProgressD();
            }

            @Override
            public void onFailure(VolleyError error) {
                super.onFailure(error);
                model.showDailog("获取数据失败，请检查网络");
            }

            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                try {
                    if (null != t) {
                        BuildBean bean = new GsonUtils().toBean(t, BuildBean.class);
                        if (null != bean && bean.getCode() == 0) {
                            model.updateList(bean.getResult());
                        } else {
                            model.showDailog("未知错误");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    model.showDailog("获取数据失败，联系管理员");
                }


            }
        });

    }
}
